package cn.bugstack.domain.activity.service.trial.node;

import cn.bugstack.domain.activity.model.entity.MarketProductEntity;
import cn.bugstack.domain.activity.model.entity.TrialBalanceEntity;
import cn.bugstack.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.bugstack.domain.activity.model.valobj.SkuVO;
import cn.bugstack.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import cn.bugstack.domain.activity.service.trial.factory.DefaultActivityStrategyFactory.DynamicContext;
import cn.bugstack.types.design.framework.tree.StrategyHandler;
import com.alibaba.fastjson.JSON;
import java.math.BigDecimal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author hsj
 * @date 2025/2/12 11:01
 */
@Slf4j
@Service
public class EndNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DynamicContext, TrialBalanceEntity> {

	@Override
	public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DynamicContext dynamicContext) {

		log.info("拼团商品查询试算服务-EndNode userId:{} requestParameter:{}", requestParameter.getUserId(), JSON.toJSONString(requestParameter));

		// 拼团活动配置
		GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();

		// 商品信息
		SkuVO skuVO = dynamicContext.getSkuVO();

		// 折扣金额
		BigDecimal deductionPrice = dynamicContext.getDeductionPrice();
		// 支付金额
		BigDecimal payPrice = dynamicContext.getPayPrice();

		// 返回空结果
		return TrialBalanceEntity.builder()
		                         .goodsId(skuVO.getGoodsId())
		                         .goodsName(skuVO.getGoodsName())
		                         .originalPrice(skuVO.getOriginalPrice())
		                         .deductionPrice(deductionPrice)
		                         .payPrice(payPrice)
		                         .targetCount(groupBuyActivityDiscountVO.getTarget())
		                         .startTime(groupBuyActivityDiscountVO.getStartTime())
		                         .endTime(groupBuyActivityDiscountVO.getEndTime())
		                         .isVisible(dynamicContext.isVisible())
		                         .isEnable(dynamicContext.isEnable())
		                         .groupBuyActivityDiscountVO(groupBuyActivityDiscountVO)
		                         .build();

	}

	@Override
	public StrategyHandler<MarketProductEntity, DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter,
		DynamicContext dynamicContext) {
		return defaultStrategyHandler;
	}

}
